#pragma once

#include <iostream>
#include <pthread.h>

using namespace std;

namespace LockModule
{
    class Mutex
    {
    public:
        Mutex(const Mutex&) = delete;  // 禁止拷贝构造
        const Mutex& operator=(const Mutex&) = delete;  // 禁止赋值拷贝
        Mutex()
        {
            int n = ::pthread_mutex_init(&_lock, nullptr);
            (void)n;
        }
        ~Mutex()
        {
            int n = ::pthread_mutex_destroy(&_lock);
            (void)n;
        }
        void Lock()
        {
            int n = ::pthread_mutex_lock(&_lock);
            (void)n;
        }
        void Unlock()
        {
            int n = ::pthread_mutex_unlock(&_lock);
            (void)n;
        }
        pthread_mutex_t* GetLockPtr()
        {
            return &_lock;
        }
    private:
        pthread_mutex_t _lock;  // 互斥锁
    };

    class LockGuard  // 封装锁保护  std::lock_guard
    {
    public:
        LockGuard(Mutex& mtx)
            :_mtx(mtx)
        {
            _mtx.Lock();
        }
        ~LockGuard()
        {
            _mtx.Unlock();
        }
    private:
        Mutex& _mtx;
    };
}